package cn.fengzhilun.javase.day11;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import cn.fengzhilun.javase.day10.ex.Student;

public class TestList {
	@Test
	public void test1() {
		//int[] array= findPrimesIn100();
		//System.out.println(Arrays.toString(array));
		System.out.println(findPrimesIn100());
	}
	
	public static List<Integer> findPrimesIn100() {
		//int [] result =new int[100];
		List<Integer> result=new ArrayList<Integer>();
		int index=0;
		
		for(int i=2;i<=100;i++) {
			
			if(isPrime(i)) {
//				result[index]=i;
//				index++;
				result.add(i);
			}
		}
		
//		int[] data=new int[index];
//		for(int i=0;i<index;i++) {
//			data[i]=result[i];
//		}
		
		return result;
	}
	
	private static boolean isPrime(int number) {
		boolean flag=true;
		for(int i=2;i<number/2; i++) {
			if(number%i==0) {
				return false;
			}
		}
		return true;
	}
	@Test
	public void test3() {
		List<String> list =new ArrayList<>();
		
		list.add("abc");
		list.add("bcd");
		
		System.out.println(list.get(0));
		System.out.println(list.get(1));
		
		list.add(0,"def");
		System.out.println(list);
		
		System.out.println(list.size());
		
		list.remove(list.size()-1);
		System.out.println(list);
		System.out.println(list.size());
		System.out.println(list.contains("abc"));
		System.out.println(list.contains("abv"));
		
		String [] array= {};
		for(String element:array) {}
		
		String s="abc";
		for(int i=0;i<s.length();i++) {
			System.out.println(s.charAt(i));
		}
//		for(char i:s) {
//			System.out.println(i);
//		}
	}
	@Test
	public void test() {
		List<String> list=new ArrayList<>();
		list.add("abc");
		list.add("bcd");
		list.add("cde");
		
		System.out.println(list.size());
		System.out.println(list.get(2));
		list.add(3, "def");
		list.remove("abc");
		System.out.println(list);
		System.out.println(list.size());
		
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i));
			
		}
		
		for(String i:list) {
			System.out.println(i);
		}
	}
	@Test
	public void test4() {
		List<String> list = Arrays.asList("abc","bcd","cdf");
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i));
			
		}
		for(String i:list) {
			System.out.println(i);
		}
		Iterator<String> itr= list.iterator();
		while(itr.hasNext()) {
			System.out.println(itr.next());
		}
	}
	
	@Test
	public void test5() {
		//List<String> list = Arrays.asList("张三","李四","王五");
		List<String> list=new ArrayList<>();
		
		list.add("张三");
		list.add("李四");
		list.add("王五");
		Iterator<String> itr= list.iterator();
		
		while(itr.hasNext()) {
			System.out.println(itr.next());
		}
		
		for(int i=0;i<list.size();i++) {
			String name =list.get(i);
			
			if(name.charAt(0)=='王') {
				list.remove(i);
			}
		}
		System.out.println(list);
	}
	@Test
	public void test6() {
		List<String> list =new ArrayList<>();
		list.add("张三");
		list.add("李四");
		list.add("王五");
		
//		for(String name:list) {
//			if(name.charAt(0)=='王') {
//				list.remove(name);
//			}
//		}
		Iterator<String> itr= list.iterator();
		while(itr.hasNext()) {
			if (itr.next().charAt(0)=='王') {
				itr.remove();
				
			}
		}
		
		System.out.println(list);
	}
	@Test
	public void test7() {
//		List<String> list =new ArrayList<>();
//		list.add("张三");
//		list.add("李四");
//		list.add("王五");
//		
//		Iterator<String> itr= list.iterator();
//		
//		while(itr.hasNext()) {
//			String name = itr.next();
//			System.out.println(name);
//			if(name.charAt(0)=='王') {
//				itr.remove();
//			}
//		}
//		System.out.println(list);
	}
	@Test
	public void test8() {
		List<String> list = Arrays.asList("bbb","aaa","ccc","ddd");
		
		Collections.sort(list);
		
		System.out.println(list);
	}
	@Test
	public void test9() {
		List<Student> list =Arrays.asList(
			new Student("张三",77),	
			new Student("李四",60),	
			new Student("王五",99),	
			new Student("赵六",80)
				);
		Collections.sort(list,new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				return (o1.getAge()-o2.getAge())>0?1:-1;
			}
			
			
		});
		System.out.println(list);
	}
	@Test
	public void test10() {
		List<Student> list =Arrays.asList(
				new Student("张三",77),	
				new Student("李四",60),	
				new Student("王五",99),	
				new Student("赵六",80)
					);
		Collections.sort(list);
		
		System.out.println(list);
	}
	@Test
	public void test11() {
		Set<String> set=new HashSet<String>();
		set.add("ab");
		set.add("ab");
		System.out.println(set);
		System.out.println(set.size());
	}
	@Test
	public void test12() {
		String source="list set arraylist hashset set list";
		
		String [] split =source.split(" ");
		System.out.println(Arrays.toString(split));
		Set<String> set= new HashSet();
		for(int i=0;i<split.length;i++) {
			set.add(split[i]);
		}
		System.out.println(set.size());
		System.out.println(set);
	}
}
